package com.hsh.dao.impl;

import com.hsh.dao.UserDao;
import com.hsh.pojo.Page;
import com.hsh.pojo.User;
import com.hsh.utils.MySQLConnectUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xrkhy
 * @date 2025/6/27 8:50
 * @description
 */
public class UserDaoImpl implements UserDao {
    @Override
    public boolean queryUserByName(String userName) {
        String spl  = "select id from user where userName = ?";
        Object[] obj = new Object[]{ userName };
//        MySQLConnectUtils.executeQuery(spl,obj,User.class);
        return false;
    }

    @Override
    public User login(User  user) {
        String sql = "select * from user where userName = ? and userPassword = ?";
        Object[] params = {user.getUserName(), user.getUserPassword()};
        System.out.println(user);

        User resUser = MySQLConnectUtils.executeQueryForObject(sql, params, User.class);

        return resUser;
    }

    @Override
    public int register(User user) {
        String sql = "insert into user (userName,userPassword) values (?,?)";
        Object[] obj = new Object[]{ user.getUserName(),user.getUserPassword() };
        int i = MySQLConnectUtils.executeUpdate(sql,obj);

        return i;
    }

    @Override
    public List<User> pageQuery(Page<User> page) {
        String sql = "select id,userName,userPassword,role,age from user ";
        String limit = " limit ?,?";
        String str = "";
        Object[] obj = new Object[]{page.getStartIndex(),page.getPageSize()};
        User condition = page.getCondition();

        if(condition.getId() != null){
            str += " and id = " + condition.getId();
        }

        if(condition.getUserName() != null){
            str += " and userName like \'%" + condition.getUserName() + "%\' ";
        }

        if (str != ""){
            // 将前四位字符 " and" 删除
            str = str.substring(4);
            str = " where " + str;
        }

        sql += str + limit;

        System.out.println(sql);


        List<User> lists = MySQLConnectUtils.executeQueryForList(sql,obj,User.class);
        return lists;
    }

    @Override
    public Integer queryTotalCount(Page<User> page) {
        String sql = "select count(*) from user";
        String str = "";
        User condition = page.getCondition();

        if(condition.getId() != null){
            str += " and id = " + condition.getId();
        }

        if(condition.getUserName() != null){
            str += " and userName like \'%" + condition.getUserName() + "%\' ";
        }

        if (str != ""){
            // 将前四位字符 " and" 删除
            str = str.substring(4);
            str = " where " + str;
        }
        sql += str;
        System.out.println("count:" +sql);
        Integer totalCount = MySQLConnectUtils.queryTotalCount(sql,null);
        return totalCount;
    }

    @Override
    public Integer deleteUser(Integer id) {
        String sql = "delete from user where id = ?";
        Object[] params = new Object[]{id};
        Integer deleteCount = MySQLConnectUtils.executeUpdate(sql,params);
        return deleteCount;
    }

    @Override
    public Integer updateUser(User user) {
        String sql = "update user set userName = ?,userPassword = ?,age = ?,role = ? where id = ?";
        Object[] obj = new Object[]{ user.getUserName(),user.getUserPassword(),user.getAge(),user.getRole(),user.getId() };
        int i = MySQLConnectUtils.executeUpdate(sql,obj);
        return i;
    }
}
